package ckf.man.practice.controller;

import ckf.man.practice.config.DataStatusType;
import ckf.man.practice.entity.DataStatus;
import ckf.man.practice.entity.Dto.DataStatusDto;
import ckf.man.practice.service.DataStatusService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/data_status")
public class DataStatusController {
    @Autowired
    private DataStatusService service;

    private List<DataStatusDto> transferDataDto(List<DataStatus> records) {
        List<DataStatusDto> res = new ArrayList<>();
        for (DataStatus record : records) {
            int status = record.getStatus();
            DataStatusDto dto = new DataStatusDto();
            BeanUtils.copyProperties(record, dto);
            if (status == DataStatusType.ORIGIN.getStatus()) {
                dto.setStatusName(DataStatusType.ORIGIN.getType());
            }

            if (status == DataStatusType.AUDIT.getStatus()) {
                dto.setStatusName(DataStatusType.AUDIT.getType());
            }
            res.add(dto);
        }

        return res;
    }

    @GetMapping("/get")
    public List<DataStatusDto> get() {
        List<DataStatus> results = service.list();
        return transferDataDto(results);

    }

    @PostMapping("/getByDataId")
    public List<DataStatusDto> getByDataId(@RequestBody Map<String, Integer> data) {
        List<DataStatus> results = service.getByDataId(data.get("data_id"));
        return transferDataDto(results);

    }

    @PostMapping("/addOrUpdate")
    public String addOrUpdate(@RequestBody Map<String, List<DataStatus>> data) {
        List<DataStatus> list = data.get("list");
        boolean success = service.saveOrUpdateBatch(list);
        return success ?  "修改成功":"修改失败";
    }

    @PostMapping("/deleteByIds")
    public String deleteByIds(@RequestBody Map<String, List<Integer>> data) {
        List<Integer> ids = data.get("ids");
        boolean success = service.removeBatchByIds(ids);
        return success ? "删除成功":"删除失败";
    }

}
